﻿using CourseSelectionSystem.Model.EF;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CourseSelectionSystem.Service
{
    public partial class CourseSelectionSystemServices : Form
    {
        public CourseSelectionSystemServices()
        {
            InitializeComponent();
        }

        private void CourseSelectionSystemServices_Load(object sender, EventArgs e)
        {

        }
    }

    
}

internal class CourseSelectionSystemServices
{
    /// <summary>
    /// 获得所有数据
    /// </summary>
    /// <returns></returns>
    public List<Student> GetAlls()
    {
        var db = new CourseSelectionSystem2024Entities1();
        return db.Student.ToList();
    }

    public List<Teacher> GetAllt()
    {
        var db = new CourseSelectionSystem2024Entities1();
        return db.Teacher.ToList();
    }

    public List<Course> GetAllc()
    {
        var db = new CourseSelectionSystem2024Entities1();
        return db.Course.ToList();
    }

    /// <summary>
    /// 新增学生信息
    /// </summary>
    /// <param name="model"></param>
    /// <exception cref="ArgumentException"></exception>
    /// <exception cref="InvalidOperationException"></exception>
    /// <exception cref="Exception"></exception>
    public void Creates(CreateModels models)
    {
        // 确保传入的 ID 是非空且符合预期长度或格式
        if (string.IsNullOrWhiteSpace(models.ID))
        {
            MessageBox.Show("ID格式有误,请检查","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
        var db = new CourseSelectionSystem2024Entities1();

        // 检查是否已经有相同的 ID 存在于数据库中
        if (db.Student.Any(x => x.ID == models.ID))
        {
            MessageBox.Show("该ID已存在，请检查","提示",MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        var newItem = new Student()
        {
            ID = models.ID,
            Name = models.Name,
            Class = models.Class,
            Password = models.Password,
            Hometown = models.Hometown,
        };
        db.Student.Add(newItem);
        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException ex)
        {
            // 处理可能发生的数据库更新错误，如唯一约束冲突等
            throw new Exception("Failed to create the address due to a database error.", ex);
        }
    }

    /// <summary>
    /// 新增教师信息
    /// </summary>
    /// <param name="model"></param>
    /// <exception cref="ArgumentException"></exception>
    /// <exception cref="InvalidOperationException"></exception>
    /// <exception cref="Exception"></exception>
    public void Createt(CreateModelt modelt)
    {
        // 确保传入的 ID 是非空且符合预期长度或格式
        if (string.IsNullOrWhiteSpace(modelt.ID))
        {
            MessageBox.Show("ID格式有误,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        var db = new CourseSelectionSystem2024Entities1();

        // 检查是否已经有相同的 ID 存在于数据库中
        if (db.Teacher.Any(x => x.ID == modelt.ID))
        {
            MessageBox.Show("该ID已存在，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        var newItem = new Teacher()
        {
            ID = modelt.ID,
            Name = modelt.Name,
            Title = modelt.Title,
            Password = modelt.Password,
        };
        db.Teacher.Add(newItem);
        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException ex)
        {
            // 处理可能发生的数据库更新错误，如唯一约束冲突等
            throw new Exception("Failed to create the address due to a database error.", ex);
        }
    }

    /// <summary>
    /// 新增课程信息
    /// </summary>
    /// <param name="model"></param>
    /// <exception cref="ArgumentException"></exception>
    /// <exception cref="InvalidOperationException"></exception>
    /// <exception cref="Exception"></exception>
    public void Createc(CreateModelc modelc)
    {
        // 确保传入的 ID 是非空且符合预期长度或格式
        if (string.IsNullOrWhiteSpace(modelc.ID))
        {
            MessageBox.Show("ID格式有误,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        var db = new CourseSelectionSystem2024Entities1();

        // 检查是否已经有相同的 ID 存在于数据库中
        if (db.Course.Any(x =>x.ID == modelc.ID))
        {
            MessageBox.Show("该ID已存在，请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        var newItem = new Course()
        {
            ID = modelc.ID,
            Name = modelc.Name,
            Teacher = modelc.Teacher,
            Credit = modelc.Credit,
        };
        db.Course.Add(newItem);
        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException ex)
        {
            // 处理可能发生的数据库更新错误，如唯一约束冲突等
            throw new Exception("Failed to create the address due to a database error.", ex);
        }
    }

    /// <summary>
    /// 获取要修改的学生信息
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public Student GetBysID(string ID)
    {
        var db = new CourseSelectionSystem2024Entities1();
        return db.Student.Find(ID);
    }

    /// <summary>
    /// 修改学生信息
    /// </summary>
    /// <param name="models"></param>
    public void Edits(EditModels models)
    {
        var db = new CourseSelectionSystem2024Entities1();
        var item = db.Student.Find(models.ID);
        if(item!= null)
        {
            item.ID = models.ID;
            item.Name = models.Name;
            item.Class = models.Class;
            item.Password = models.Password;
            item.Hometown = models.Hometown;
        }
        db.SaveChanges();
    }

    /// <summary>
    /// 获取要修改的教师信息
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public Teacher GetBytID(string ID)
    {
        var db = new CourseSelectionSystem2024Entities1();
        return db.Teacher.Find(ID);
    }

    /// <summary>
    /// 修改教师信息
    /// </summary>
    /// <param name="modelt"></param>
    public void Editt(EditModelt modelt)
    {
        var db = new CourseSelectionSystem2024Entities1();
        var item = db.Teacher.Find(modelt.ID);
        if(item != null)
        {
            item.ID = modelt.ID;
            item.Name = modelt.Name;
            item.Title = modelt.Title;
            item.Password = modelt.Password;
        }
        db.SaveChanges();
    }

    /// <summary>
    /// 获取要修改的课程信息
    /// </summary>
    /// <param name="ID"></param>
    /// <returns></returns>
    public Course GetBycID(string ID)
    {
        var db = new CourseSelectionSystem2024Entities1();
        return db.Course.Find(ID);
    }

    /// <summary>
    /// 修改课程信息
    /// </summary>
    /// <param name="modelc"></param>
    public void Editc(EditModelc modelc)
    {
        var db = new CourseSelectionSystem2024Entities1();
        var item = db.Course.Find(modelc.ID);
        if(item != null)
        {
            item.ID = modelc.ID;
            item.Name = modelc.Name;
            item.Teacher = modelc.Teacher;
            item.Credit = modelc.Credit;
        }
        db.SaveChanges();
    }

    /// <summary>
    /// 删除学生信息
    /// </summary>
    /// <param name="ID"></param>
    public void Deletes(string ID)
    {
        var db = new CourseSelectionSystem2024Entities1();
        var item = db.Student.Find(ID);
        if (item != null)
        {
            db.Student.Remove(item);
            db.SaveChanges();
        }
    }

    /// <summary>
    /// 删除教师信息
    /// </summary>
    /// <param name="ID"></param>
    public void Deletet(string ID)
    {
        var db = new CourseSelectionSystem2024Entities1();
        var item = db.Teacher.Find(ID);
        if( item != null)
        {
            db.Teacher.Remove(item);
            db.SaveChanges();
        }
    }

    /// <summary>
    /// 删除课程信息
    /// </summary>
    /// <param name="ID"></param>
    public void Deletec(string ID)
    {
        var db = new CourseSelectionSystem2024Entities1();
        var item = db.Course.Find(ID);
        if (item != null)
        {
            db.Course.Remove(item);
            db.SaveChanges();
        }
    }
}
